﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace WorkStation.Frameworks.Core.Data.DataTableValidate.Rules
{
    /// <summary>
    /// * 数据表列格式验证
    /// </summary>
    public class DataTableFormatRule : TableRuleBase
    {
        public List<string> Columns { get; set; }

        public DataTableFormatRule(List<string> columns) 
        {
            this.Columns = columns;
        }

        public override ValidateResult Execute(DataTable table)
        {
            ValidateResult res = new ValidateResult() { IsSuccess = true };

            var columns = table.Columns.Cast<DataColumn>().ToList();

            foreach (DataColumn column in columns) 
            {
                if (this.Columns.Where(v => column.ColumnName.Equals(v)).Count() == 0) 
                {
                    res.IsSuccess = false;
                    res.Msg = "数据表格式不正确";
                    break;
                }
            }

            return res;
        }
    }
}
